package com.haircut.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.haircut.base.Result;
import com.haircut.mapper.HaircutUserOrder;
import com.haircut.mapper.WechatUser;
import com.haircut.model.dto.UserDTO;
import com.haircut.model.dto.UserOrderDTO;
import com.haircut.model.dto.UserOrderResp;
import com.haircut.model.req.OrderListReq;
import com.haircut.service.HaircutUserOrderService;
import com.haircut.mapper.HaircutUserOrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author Administrator
* @description 针对表【t_haircut_user_order用户消费记录表)】的数据库操作Service实现
* @createDate 2025-11-02 14:41:42
*/
@Service
@Slf4j
public class HaircutUserOrderServiceImpl extends ServiceImpl<HaircutUserOrderMapper, HaircutUserOrder>
    implements HaircutUserOrderService {


    @Override
    public List<HaircutUserOrder> getOrders(Long userId,String phone) {
        LambdaQueryWrapper<HaircutUserOrder> queryWrapper = new LambdaQueryWrapper<>();
        if(ObjectUtil.isNotNull(userId)){
            queryWrapper.eq(HaircutUserOrder::getUserId, userId);
        }
        if(ObjectUtil.isNotNull(phone)){
            queryWrapper.eq(HaircutUserOrder::getPhone, phone);
        }
        return list(queryWrapper);
    }

    @Override
    public Result<UserOrderResp> getOrders(OrderListReq req) {
        LambdaQueryWrapper<HaircutUserOrder> queryWrapper = new LambdaQueryWrapper<>();

        log.info("getOrders"+req.getPageNum()+"-"+req.getPageSize()+"-"+req.getPhone(),"");
        if(ObjectUtil.isNotEmpty(req.getPhone())){
            queryWrapper.eq(HaircutUserOrder::getPhone, req.getPhone());
        }
        queryWrapper.orderByDesc(HaircutUserOrder::getCreateTime);
        UserOrderResp resp = new UserOrderResp();

        Page<HaircutUserOrder> page = new Page<>(req.getPageNum(), req.getPageSize());
        Page<HaircutUserOrder> result = baseMapper.selectPage(page,queryWrapper);
        List<UserOrderDTO> orderList = result.getRecords().stream()
                .map(user -> {
                    return BeanUtil.copyProperties(user,UserOrderDTO.class);
                })
                .collect(Collectors.toList());

        resp.setOrderList(orderList);
        resp.setTotal(result.getTotal());
        return Result.success(resp);
    }
}




